##Constructs sample for migrants data
rm(list = ls())
library(data.table)
library(dplyr)
library(foreign)

source('param_general.R')
load(file = paste0(datDir,'base_ld_women.RData'))

##Keep non-medicaid patients
message('Start with ',nrow(useDatAll[payer %like% 'Medicaid']),' Medicaid Admissions and ', nrow(useDatAll), ' total admissions')

useDatAll[,normalLD_indic := ifelse((yearDat ==2006 |(yearDat == 2007 & qtr !=4)),
                              msdrg %in% c(371,373),# 373=normal vaginal, 371=c-section
                              msdrg %in% c(775, 766))] ###775=normal vaginal, 766=c-section

##convert dates
useDatAll[,adate := as.Date(admitdate,format = "%m/%d/%Y")]
useDatAll[,ddate := as.Date(dischdate,format = "%m/%d/%Y")]

##Drop patients with births that are within 180 days of each other
setkey(useDatAll,maskssn,adate)
useDatAll[,NVisits := .N, by = 'maskssn']
useDatAll[NVisits > 1,dayBwVisit := as.integer(adate - c(NA, head(adate, .N-1))), by = 'maskssn']
closeVisitsDrop <- unique(useDatAll[dayBwVisit < 180,maskssn])

##Patients with inconsistent birthdays
useDatAll[,bday := adate - agedays]
useDatAll[,':=' (bdayMin = min(bday), bdayMax = max(bday)),by = 'maskssn']
bDayDrop <- unique(useDatAll[abs(bdayMin - bdayMax) > 2, maskssn])
useDatAll[,bday := max(bday),by = 'maskssn']

##Drop patients patients whose coded first birth is under 10 or who have a birth over 60
ageDrop <- unique(useDatAll[age < 10 | age > 60, maskssn])

##Identify Medicaid Patients
medicaidPat <- useDatAll[payer %like% 'Medicaid', maskssn]

##Drop patients
useDatDropPat <- useDatAll[!J(union(union(closeVisitsDrop, bDayDrop),ageDrop))]
message(paste0(length(union(union(closeVisitsDrop,bDayDrop),ageDrop)), ' Dropped Patients or ',nrow(useDatAll) -nrow(useDatDropPat) ,' Dropped Observations'))

##rename and drop variables
useDatFinal <- useDatDropPat[,list(maskssn,hosp_id = faclnbr,admission = sys_recid, adate,ddate,pat_zip = zipcode, pat_county = ptcounty, age, 
                                    black, white, hispanic,payer,
                                    atten_phyid,oper_phyid,normalLD_indic,bday,yearDat,charges, msdrg, fac_region)]

useDatFinalList <- list(all = copy(useDatFinal), medicaid = useDatFinal[payer %like% 'Medicaid'])

##flag patients in small zipcodes
flagZipFunc <- function(useDat){
    patZipDrop <- useDat[,.N, by = 'pat_zip'][N < smallZipSize, pat_zip]
    useDat[,smallZip := pat_zip %in% patZipDrop]
    return(useDat)
}

useDatFinalList <- lapply(useDatFinalList, flagZipFunc)

saveRDS(useDatFinalList$all,file = paste0(datDir,'ld_women_process_all.rds'))
